package nl.avans.osgi.consoleclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import nl.avans.osgi.clientmodel.ClientModelService;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class ClientServiceTrackerCustomizer implements ServiceTrackerCustomizer {

    private BundleContext context;
    private ServiceReference reference;

    public ClientServiceTrackerCustomizer(BundleContext context) {
        this.context = context;
    }
    
    @Override
    public Object addingService(ServiceReference reference) {
        this.reference = reference;
        final ClientModelService service = (ClientModelService) context.getService(reference);
        String data = "new data from ClientServiceTrackerCustomizer while addingService";
        System.out.println(String.format("customizer: adding service for: %s with data set: %s", service.getClass().getCanonicalName(), data));
        service.setData(data);
        
        Thread mythread = new MyThread(service);
        mythread.start();

        return mythread;
    }

    @Override
    public void modifiedService(ServiceReference reference, Object service) {
        ClientModelService clientService = (ClientModelService) context.getService(reference);
        clientService.setData(String.format("Service %s is modified", service.getClass().getName()));
    }

    @Override
    public void removedService(ServiceReference reference, Object service) {
        ClientModelService clientService = (ClientModelService) context.getService(reference);
        clientService.setData(String.format("Service %s will be removed", clientService.getClass().getName()));
        
        MyThread mythread = (MyThread) service;
        mythread.stopThread();
        
        context.ungetService(reference);
    }
    
    private class MyThread extends Thread {
        private ClientModelService service;
        private boolean active = true;
        
        public MyThread(ClientModelService service) {
            this.service = service;
        }
        
        @Override
        public void run() {
            System.out.println("type exit to exit");
            String input = "";
            
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                while (active) {
                    System.out.println("Enter some text.");
                    input = reader.readLine();
                    
                    if ("exit".equals(input)) {
                        active = false;
                        context.getBundle().stop();
                        break;
                    }
                    service.setData(input);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public void stopThread() {
            active = false;
        }
    }
}
